﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Linq;

namespace EntryExit
{

    public partial class DashboardReport : DevExpress.XtraReports.UI.XtraReport
    {
        private int ColumnCount { get; set; }

        //public DashboardReport(string reportSubject, DataTable data)
        public DashboardReport(DataTable data, DataTable data2, DataTable LeaveMarked, DataTable Offday)
        {
            InitializeComponent();
            ClearDataContext();
            foreach (DataRow dr in data.Rows)
            {
                foreach (DataColumn dc in data.Columns)
                {
                    if (dr[dc.ColumnName] == DBNull.Value)
                    {
                        TimeSpan s = new TimeSpan();
                        s = TimeSpan.MinValue;
                        dr[dc.ColumnName] = s;
                    }
                }
            }
            Dictionary<string, string> attendanceDetails = new Dictionary<string, string>();
            Dictionary<int, string> Header = new Dictionary<int, string>();
            Dictionary<string, string> OffdayDict = new Dictionary<string, string>();
            
            //lblRptHead.Text = reportSubject;
            if (LeaveMarked != null && LeaveMarked.Rows.Count > 0)
            {
                foreach (DataRow row in LeaveMarked.Rows)
                {
                    string eventDate = (Convert.ToDateTime(row["ShiftDate"]).Date.ToString("dd MMM yyyy") + "|" + row["EmployeeName"]).Trim();



                    attendanceDetails.Add(eventDate.ToUpper(), "");

                }
            }
            if (Offday != null && Offday.Rows.Count > 0)
            {
                foreach (DataRow row in Offday.Rows)
                {
                    string OffDate = (Convert.ToDateTime(row["WeekendDate"]).Date.ToString("dd MMM yyyy")).Trim();
                    OffdayDict.Add(OffDate.ToUpper(), "");
                }
            }
            if(data.Columns.Count>1)
            Header.Add(1, data.Columns[1].ColumnName.ToUpper());
            if (data.Columns.Count > 2)
            Header.Add(2, data.Columns[2].ColumnName.ToUpper());
            if (data.Columns.Count > 3)
            Header.Add(3, data.Columns[3].ColumnName.ToUpper());
            if (data.Columns.Count > 4)
            Header.Add(4, data.Columns[4].ColumnName.ToUpper());
            if (data.Columns.Count > 5)
            Header.Add(5, data.Columns[5].ColumnName.ToUpper());
            if (data.Columns.Count > 6)
            Header.Add(6, data.Columns[6].ColumnName.ToUpper());
            if (data.Columns.Count > 7)
            Header.Add(7, data.Columns[7].ColumnName.ToUpper());
            if (data.Columns.Count > 8)
            Header.Add(8, data.Columns[8].ColumnName.ToUpper());
            if (data.Columns.Count > 9)
            Header.Add(9, data.Columns[9].ColumnName.ToUpper());
            if (data.Columns.Count > 10)
            Header.Add(10, data.Columns[10].ColumnName.ToUpper());
            if (data.Columns.Count > 11)
            Header.Add(11, data.Columns[11].ColumnName.ToUpper());
            HttpContext.Current.Session["Caption"] = Header;
            HttpContext.Current.Session["EmpLeave"] = attendanceDetails;
            HttpContext.Current.Session["OffdayDict"] = OffdayDict;
            if (data != null)
            {
                BindLabels(data);
            }
        }

       
        private void xrTableCell9_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            XRTableCell cell = sender as XRTableCell;
            cell.Text = GetCurrentColumnValue("PostalCode").ToString() + " some text";
        }
        private void BindLabels(DataTable data)
        {
            this.DataSource = data;
            ColumnCount = 0;
            if (data.Rows.Count > 0)
            {

                // For Complete the column same as report

                ColumnCount =data.Columns.Count;
                for (int Count = data.Columns.Count; Count < 12; Count++)
                {
                    data.Columns.Add();
                    data.Columns[Count - 1].DefaultValue = 0;
                }

                cell1.Text = data.Columns[1].ColumnName;
                cell2.Text = data.Columns[2].ColumnName;
                cell3.Text = data.Columns[3].ColumnName;
                cell4.Text = data.Columns[4].ColumnName;
                cell5.Text = data.Columns[5].ColumnName;
                cell6.Text = data.Columns[6].ColumnName;
                cell7.Text = data.Columns[7].ColumnName;
                cell8.Text = data.Columns[8].ColumnName;
                cell9.Text = data.Columns[9].ColumnName;
                cell10.Text = data.Columns[10].ColumnName;
                cell11.Text = data.Columns[11].ColumnName;
               

                XRBinding bindingName = new XRBinding("Text", data, data.Columns[0].ColumnName);
                XRBinding bindingDt1 = new XRBinding("Text",  data, data.Columns[1].ColumnName);
                XRBinding bindingDt2 = new XRBinding("Text", data, data.Columns[2].ColumnName);
                XRBinding bindingDt3 = new XRBinding("Text", data, data.Columns[3].ColumnName);
                XRBinding bindingDt4 = new XRBinding("Text", data, data.Columns[4].ColumnName);
                XRBinding bindingDt5 = new XRBinding("Text", data, data.Columns[5].ColumnName);
                XRBinding bindingDt6 = new XRBinding("Text", data, data.Columns[6].ColumnName);
                XRBinding bindingDt7 = new XRBinding("Text", data, data.Columns[7].ColumnName);
                XRBinding bindingDt8 = new XRBinding("Text", data, data.Columns[8].ColumnName);
                XRBinding bindingDt9 = new XRBinding("Text", data, data.Columns[9].ColumnName);
                XRBinding bindingDt10 = new XRBinding("Text", data, data.Columns[10].ColumnName);
                XRBinding bindingDt11 = new XRBinding("Text", data, data.Columns[11].ColumnName);
               
                datacellname.DataBindings.Add(bindingName);
                datacell1.DataBindings.Add(bindingDt1);
                datacell2.DataBindings.Add(bindingDt2);
                datacell3.DataBindings.Add(bindingDt3);
                datacell4.DataBindings.Add(bindingDt4);
                datacell5.DataBindings.Add(bindingDt5);
                datacell6.DataBindings.Add(bindingDt6);
                datacell7.DataBindings.Add(bindingDt7);
                datacell8.DataBindings.Add(bindingDt8);
                datacell9.DataBindings.Add(bindingDt9);
                datacell10.DataBindings.Add(bindingDt10);
                datacell11.DataBindings.Add(bindingDt11);

                //For hiding extra columns

                for (int Count = 12; Count > ColumnCount-1; Count--)
                {
                   // xrTable1.SuspendLayout();
                    xrTableRow1.Cells.Remove(xrTableRow1.Cells[Count-1]);
                    xrTableRow2.Cells.Remove(xrTableRow2.Cells[Count-1]);
                    //xrTable1.PerformLayout();
                }
                

                List<TimeSpan> times = new List<TimeSpan>();
                HttpContext.Current.Session["times"] = times;
            }
        }


        private void textChanged(object sender, EventArgs e)
        {
            if ((this.ColumnCount - 2) == ((XRTableCell)sender).Index)
            {
                ((XRTableCell)sender).BackColor = Color.White;
            }
            else
            {
                if (((XRTableCell)sender).Text != null && ((XRTableCell)sender).Text != "" && ((XRTableCell)sender).Text != "L")
                {
                    List<TimeSpan> t = (List<TimeSpan>)HttpContext.Current.Session["times"];
                    if (((XRTableCell)sender).Text != "")
                    {
                        TimeSpan time;
                        TimeSpan.TryParse(((XRTableCell)sender).Text, out time);
                        if (time != TimeSpan.MinValue)
                        {
                            t.Add(time);
                        }
                        HttpContext.Current.Session["times"] = t;
                        Dictionary<string, string> attendanceDetails = new Dictionary<string, string>();
                        Dictionary<int, string> Header = new Dictionary<int, string>();
                        Dictionary<string, string> OffdayDict = new Dictionary<string, string>();
                        if (HttpContext.Current.Session["Caption"] != null)
                        {
                            Header = (Dictionary<int, string>)HttpContext.Current.Session["Caption"];
                        }
                        if (HttpContext.Current.Session["EmpLeave"] != null)
                        {
                            attendanceDetails = (Dictionary<string, string>)HttpContext.Current.Session["EmpLeave"];
                        }
                        if (HttpContext.Current.Session["OffdayDict"] != null)
                        {
                            OffdayDict = (Dictionary<string, string>)HttpContext.Current.Session["OffdayDict"];
                        }
                        if (Header.ContainsKey(((XRTableCell)sender).Index))
                        {
                            if (OffdayDict.ContainsKey(Header[(((XRTableCell)sender).Index)].ToUpper()))
                            {
                                ((XRTableCell)sender).Text = "";
                                time = TimeSpan.MinValue;
                            }
                        }
                        if (time == TimeSpan.MinValue)
                        {
                            try
                            {

                                ((XRTableCell)sender).Text = "";

                                if (Header.ContainsKey(((XRTableCell)sender).Index))
                                {
                                    if (OffdayDict.ContainsKey(Header[(((XRTableCell)sender).Index)].ToUpper()))
                                    {
                                        ((XRTableCell)sender).BackColor = Color.White;
                                    }
                                    else if (attendanceDetails.ContainsKey((Header[(((XRTableCell)sender).Index)] + "|" + GetCurrentColumnValue("employeename").ToString()).ToUpper()))
                                    {
                                        ((XRTableCell)sender).Text = "L";


                                        ((XRTableCell)sender).BackColor = Color.LightBlue;
                                    }
                                    else
                                    {
                                        ((XRTableCell)sender).BackColor = Color.Red;
                                    }
                                }
                                else
                                {
                                    ((XRTableCell)sender).BackColor = Color.Red;
                                }
                            }
                            catch
                            {
                            }
                        }
                        else if (time.Hours >= 9)
                        {
                            ((XRTableCell)sender).BackColor = Color.Yellow;
                        }
                        else if (time.Hours >= 8 && time.Hours < 9)
                            ((XRTableCell)sender).BackColor = Color.LawnGreen;
                        else
                            if (time.Hours >= 7 && time.Hours < 8)
                            {
                                if (Header.ContainsKey(((XRTableCell)sender).Index))
                                {
                                    if (attendanceDetails.ContainsKey((Header[(((XRTableCell)sender).Index)] + "|" + GetCurrentColumnValue("employeename").ToString()).ToUpper()))
                                    {

                                        ((XRTableCell)sender).BackColor = Color.GreenYellow;
                                    }
                                    else
                                    {
                                        ((XRTableCell)sender).BackColor = Color.Orange;
                                    }
                                }
                                else
                                {
                                    ((XRTableCell)sender).BackColor = Color.Orange;
                                }

                            }
                            else
                                if (time.Hours >= 4 && time.Hours < 7)
                                {
                                    if (Header.ContainsKey(((XRTableCell)sender).Index))
                                    {
                                        if (attendanceDetails.ContainsKey((Header[(((XRTableCell)sender).Index)] + "|" + GetCurrentColumnValue("employeename").ToString()).ToUpper()))
                                        {

                                            ((XRTableCell)sender).BackColor = Color.GreenYellow;
                                        }
                                        else
                                        {
                                            ((XRTableCell)sender).BackColor = Color.Pink;
                                        }
                                    }
                                    else
                                    {
                                        ((XRTableCell)sender).BackColor = Color.Pink;
                                    }
                                }
                                else
                                    if (time.Hours < 4)
                                    {
                                        if (Header.ContainsKey(((XRTableCell)sender).Index))
                                        {
                                            if (attendanceDetails.ContainsKey((Header[(((XRTableCell)sender).Index)] + "|" + GetCurrentColumnValue("employeename").ToString()).ToUpper()))
                                            {

                                                ((XRTableCell)sender).BackColor = Color.GreenYellow;
                                            }
                                            else
                                            {
                                                ((XRTableCell)sender).BackColor = Color.Aqua;
                                            }
                                        }
                                        else
                                        {
                                            ((XRTableCell)sender).BackColor = Color.Aqua;
                                        }
                                    }
                                    else
                                    {
                                        ((XRTableCell)sender).BackColor = Color.Red;
                                    }
                    }
                    else
                    {

                        ((XRTableCell)sender).BackColor = Color.Red;
                    }
                }
            }
        }

        private void datacell6_AfterPrint(object sender, EventArgs e)
        {
            List<TimeSpan> t = (List<TimeSpan>)HttpContext.Current.Session["times"];
                if (t.Count != 0)
                {
                    long averageTicks = Convert.ToInt64(t.Average(timeSpan => timeSpan.Ticks));
                    ((XRTableCell)sender).Text = new DateTime(averageTicks).ToLongTimeString().ToString();
                    t.Clear();
                    HttpContext.Current.Session["times"] = t;
                }            
        }

        

        
       

    }
}
